﻿/*
 * @author lb
 * 2012-3-27 修改NewNewsInfo ,ModifyNewsInfo,
 */
package cn.edu.zjusc.Webservice;

import java.util.List;

import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.persistence.*;

import org.jboss.wsf.spi.annotation.WebContext;

import cn.edu.zjusc.POJO.*;
import cn.edu.zjusc.Security.ProfileInterceptor;
import cn.edu.zjusc.Security.UserCave;

@Stateless
@WebService(name="OnlineRegisterNewsService",serviceName="OnlineRegisterNewsService")
@SOAPBinding(style = SOAPBinding.Style.RPC)
@WebContext(contextRoot="/ZJUSCIX/Service",urlPattern="/OnlineRegisterNewsService")
public class OnlineRegisterNewsService {
	/**
	 * 获取所有系统公告
	 * @param strName
	 * @return
	 *JQuery发送样式 {
	 	<env:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/">
   		<env:Body>
      	<web:GetAllNewsInfo>
             <person>HEHE</person>
      	</web:GetAllNewsInfo>
   		</env:Body>
		</env:Envelope>
	 * }
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<OnlineRegisterNews> GetAllNewsInfo(){
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		Query query=em.createQuery("from OnlineRegisterNews e");
		List newsList=query.getResultList();
		
		em.close();
		emf.close();
		return newsList;
	}
	
	/**
	 * 新增一条公告
	 * @param eiBean
	 * @return
	 * JQuery发送样式{
		<env:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/">
		   <env:Body>
		      <web:NewNewsInfo>
		         <newsEntry>
		            <classRoomName>324</classRoomName>
		            <duration>30</duration>
		            <id></id>
		            <note>呵呵</note>
		            <roomId>120</roomId>
		            <startTime>2010-11-11T12:00:00</startTime>
		            <subject>1</subject>
		         </newsEntry>
		      </web:NewNewsInfo>
		   </env:Body>
		</env:Envelope>
		}
	 */
	//3-27 修改前备份
//	public String NewNewsInfo(@WebParam(name = "newsEntry") OnlineRegisterNews eiBean) {
//		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
//		EntityManager em=emf.createEntityManager();
//		
//		//开始事务
//		em.getTransaction().begin();
//		em.persist(eiBean);
//		//提交事务
//		em.getTransaction().commit();
//		
//		em.close();
//		emf.close();
//		
//		return "新增公告成功";
//	}
	@Interceptors(ProfileInterceptor.class)
	public String NewNewsInfo(@WebParam(name = "uc") UserCave uc,
			@WebParam(name = "newsEntry") OnlineRegisterNews eiBean) {
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		//开始事务
		em.getTransaction().begin();
		em.persist(eiBean);
		//提交事务
		em.getTransaction().commit();
		
		em.close();
		emf.close();
		
		return "新增公告成功";
	}
	/**
	 * 修改一公告
	 * @param eiBean
	 * @return
	 * JQuery发送格式
	 * 数据库中存在ID为7的数据
	 * {
			 * <env:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/">
		   <env:Body>
		      <web:ModifyNewsInfo>
		         <newsEntry>
		            <news_id>1</news_id>
		            <news_title>yun</news_title>
		            <news_content>111</news_content>
		            <news_publishTime>2012-02-02T12:00:00</news_publishTime>
		         </newsEntry>
		      </web:ModifyNewsInfo>
		   </env:Body>
		</env:Envelope>
	 * }
	 */
	//修改前备份
//	public String ModifyNewsInfo(@WebParam(name = "newsEntry") OnlineRegisterNews eiBean) {
//		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
//		EntityManager em=emf.createEntityManager();
//		
//		//开始事务
//		em.getTransaction().begin();
//
//		em.merge(eiBean);
//		//提交事务
//		em.getTransaction().commit();
//		
//		em.close();
//		emf.close();
//		
//		return "修改系统公告成功";
//	}
	@Interceptors(ProfileInterceptor.class)
	public String ModifyNewsInfo(@WebParam(name = "uc") UserCave uc,
			@WebParam(name = "newsEntry") OnlineRegisterNews eiBean) {
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		//开始事务
		em.getTransaction().begin();

		em.merge(eiBean);
		//提交事务
		em.getTransaction().commit();
		
		em.close();
		emf.close();
		
		return "修改系统公告成功";
	}
	
	/**
	 * 删除一条公告
	 * @param news_id
	 * @return
	 * JQuery发送格式{
		<env:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/">
		   <env:Body>
		      <web:DeleteNewsInfo>
		         <id>8</id>
		      </web:DeleteNewsInfo>
		   </env:Body>
		</env:Envelope>
	 * }
	 */
	@Interceptors(ProfileInterceptor.class)
	public String DeleteNewsInfo(@WebParam(name = "uc") UserCave uc,
			@WebParam(name = "id") int newsID) {
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		//开始事务
		em.getTransaction().begin();
		System.out.println("newsID:"+newsID);
		OnlineRegisterNews eiBean=em.find(OnlineRegisterNews.class, newsID);
		em.remove(eiBean);
		//提交事务
		em.getTransaction().commit();
		
		em.close();
		emf.close();
		
		return "删除系统公告成功";
	}
	public List<OnlineRegisterNews> GetNewsInfoById(@WebParam(name = "id") int news_ID) {

		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		Query query=em.createQuery("from OnlineRegisterNews e where e.news_id="+news_ID);
		List newsList=query.getResultList();
		
		em.close();
		emf.close();
		return newsList;
	}
	
}
